package com.example.api.controller;

import com.example.api.model.entity.Distribution;
import com.example.api.repository.DistributionRepository;
import com.example.api.service.DistributionService;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrackMap;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.response.QueryTrackMapResp;
import com.kuaidi100.sdk.utils.SignUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * <h3>api</h3>
 * <p></p>
 *
 * @author : xieyongfeng
 * @date : 2023-05-20 21:21
 **/
@RestController
@RequestMapping("/api")
public class PathController {

    @Value("${path.key}")
    String key;
    @Value("${path.customer}")
    String customer;
    @Value("${path.secret}")
    String secret;
    @Value("${path.userid}")
    String userid;
    @Value("${path.num}")
    String num;
    @Value("${path.phone}")
    String phone;
    @Value("${path.from}")
    String from;

    @Resource
    private DistributionRepository distributionRepository;

    @GetMapping("/path")
    public Map getPath(@RequestParam String id) throws Exception {
        Optional<Distribution> byId = distributionRepository.findById(id);
        Distribution distribution = byId.orElse(null);
        if (distribution == null) {
            throw new RuntimeException("不存在配送");
        }
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("path",testQueryMapView(distribution.getAddress()));
        return hashMap;
    }

    public String testQueryMapView(String to) throws Exception{

        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(CompanyConstant.ZT);  // 哪家快递公司
        queryTrackParam.setNum(num);
        queryTrackParam.setPhone(phone);
        queryTrackParam.setFrom(from);
        queryTrackParam.setTo(to);
        queryTrackParam.setResultv2("2");
        String param = new Gson().toJson(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));

        IBaseClient baseClient = new QueryTrackMap();
        HttpResult result = baseClient.execute(queryTrackReq);

        QueryTrackMapResp queryTrackMapResp = new Gson().fromJson(result.getBody(),QueryTrackMapResp.class);
        System.out.println(queryTrackMapResp.getTrailUrl());
        return queryTrackMapResp.getTrailUrl();
    }
}
